const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function getSingleExpression(req, res, next) {
  logger.winston.info('expression.getSingleExpression');


  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from expressions where expression_id = ?",[req.params.expression_id]);

    	 
    	 if (rows.length >= 1){
    	 	   rows_one = rows[0];
    	 }
    	 
    	 res.status(200).json(rows_one);   
    	     	 
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }  
  

}

async function getIntentExpressions(req, res, next) {
  logger.winston.info('expression.getIntentExpressions');
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from expressions where intent_id = ?  order by expression_id desc",[req.params.intent_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  } 
    

}

async function getIntentExpressionQuery(req, res, next) {
  logger.winston.info('expression.getIntentExpressionQuery');
  var array_intentIds = req.query.intent_ids.split(","); //Very hacky due to the node-sqlite not supporting IN from an array
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from expressions where intent_id in (" + array_intentIds + ")  order by expression_id desc");
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }   
  

}

async function createIntentExpression(req, res, next) {
  logger.winston.info('expressions.createIntentExpression');
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into expressions(intent_id, expression_text) values (?,?)", [req.body.intent_id, req.body.expression_text]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
  

}

async function removeExpression(req, res, next) {
  logger.winston.info('expressions.removeExpression');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from expressions where expression_id = ?", [req.params.expression_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }
  
  
}

async function updateExpression(req, res, next) {
  logger.winston.info('expressions.updateExpressionEndpoint');
  
    try {
    	
			  const [rows,fields] = await db_promise.query("update expressions set expression_text = ? where expression_id = ?", [req.body.expression_text, req.body.expression_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  

}

module.exports = {
  getSingleExpression,
  getIntentExpressions,
  createIntentExpression,
  removeExpression,
  getIntentExpressionQuery,
  updateExpression};
